<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = item.type == 'input_number' ? ['', ''] : ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1 flex_cm">
      <div class="flex_js p_10_0">
        <div class="title">
          <span
            @click=";(top_index = 1), get_list()"
            :class="top_index == 1 ? 'select' : ''"
            >充值收款明细</span
          >
          <span
            @click=";(top_index = 2), get_list_1()"
            :class="top_index == 2 ? 'select' : ''"
            >升级收款明细</span
          >
        </div>
        <div class="flex">
          <!-- <el-button type="primary" @click="show_box = true">新增</el-button> -->

          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>

      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        highlight-current-row
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="到款日期"
          key="到款日期"
          sortable
          prop="updateTime"
          v-if="header_list[0].showTable"
        >
          <template #default="scope: any">
            <div v-if="scope.row?.updateTime">
              {{ formatDate(parseInt(scope.row?.updateTime), 'yyyy-MM-dd') }}
            </div>
            <div v-else-if="scope.row?.itemList?.length">
              {{
                formatDate(
                  parseInt(scope.row?.itemList[0]?.updateTime),
                  'yyyy-MM-dd'
                )
              }}
            </div>
            <div v-else></div>
          </template>
        </el-table-column>
        <el-table-column
          :label="top_index == 2 ? '企业名称' : '企业/个人名称'"
          key="企业/个人名称"
          sortable
          width="160px"
          prop="enterpriseName"
          v-if="header_list[1].showTable"
        >
          <template #default="scope: any">
            <div class="text_over" v-show="scope.row?.enterpriseId != '-1'">
              <el-tooltip
                :content="scope.row?.enterpriseName + ' (企业)'"
                placement="bottom"
              >
                {{ scope.row?.enterpriseName }} (企业)
              </el-tooltip>
            </div>
            <div v-show="scope.row?.enterpriseId == '-1'">
              {{ scope.row?.memberName }} (个人)
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="top_index == 2 ? '企业ID' : '企业/个人ID'"
          key="企业/个人ID"
          sortable
          prop="enterpriseId"
          v-if="header_list[2].showTable"
        >
          <template #default="scope: any">
            <div v-show="scope.row?.enterpriseId != '-1'">
              {{ scope.row?.enterpriseId }}
            </div>
            <div v-show="scope.row?.enterpriseId == '-1'">
              {{ scope.row?.memberId }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="下单日期"
          key="下单日期"
          sortable
          prop="createTime"
          v-if="header_list[3].showTable"
        >
          <template #default="scope: any">
            <div>
              {{ formatDate(parseInt(scope.row?.createTime), 'yyyy-MM-dd') }}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          :label="top_index == 1 ? '充值金额' : '升级金额'"
          key="充值金额"
          prop="totalAmount"
          sortable
          v-if="header_list[4].showTable"
        ></el-table-column>
        <el-table-column
          label="付款金额"
          key="付款金额"
          sortable
          prop="actualAmount"
          v-if="header_list[5].showTable"
        ></el-table-column>
        <el-table-column
          :label="top_index == 1 ? '充值套餐' : '升级套餐'"
          :key="top_index == 1 ? '充值套餐' : '升级套餐'"
          v-if="header_list[6].showTable"
          sortable
          prop="packageName"
        >
        </el-table-column>
        <el-table-column
          label="交易单号"
          key="交易单号"
          sortable
          prop="outTradeNo"
          v-if="header_list[7].showTable"
        >
        </el-table-column>
        <el-table-column
          label="商户单号"
          key="商户单号"
          prop="''"
          sortable
          v-if="header_list[8].showTable"
        ></el-table-column>
        <el-table-column
          label="支付方式"
          key="支付方式"
          sortable
          prop="payWay"
          v-if="header_list[9].showTable"
        >
          <template #default="scope: any">
            <div v-for="item in payWay" :key="item.id">
              <span v-if="scope.row?.payWay == item.value">{{
                item.label
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          key="备注"
          sortable
          prop="remark"
          v-if="header_list[9].showTable && top_index == 1"
        >
          <template #default="scope: any">
            <div class="text_over">
              <el-tooltip :content="scope.row?.remark" placement="bottom">
                {{ scope.row?.remark }}
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="
          (e) => {
            page_data.pageSize = e
            top_index == 1 ? get_list() : get_list_1()
          }
        "
        @current-change="
          (e) => {
            page_data.current = e
            top_index == 1 ? get_list() : get_list_1()
          }
        "
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { formatDate, dictData, handle_search_key } from '@/utils/utils' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
const list = ref()
const total = ref()
const top_index = ref(1)
const loading = ref(false)
const height_size = ref(1)
const header_list = ref<any>([
  {
    label: '到款日期',
    value: '',
    showTable: true,
    type: 'date',
    isShow: true,
    options: [],
  },
  {
    label: computed(() => {
      return top_index.value == 1 ? '企业/个人名称' : '企业名称'
    }),
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: computed(() => {
      return top_index.value == 1 ? '企业/个人ID' : '企业ID'
    }),
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '下单日期',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
  {
    label: computed(() => {
      return top_index.value == 1 ? '充值金额' : '升级金额'
    }),
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '付款金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: computed(() => {
      return top_index.value == 1 ? '充值套餐' : '升级套餐'
    }),
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: computed(() => {
      return top_index.value == 1 ? recharge_list.value : upgrade_list.value
    }),
  },
  {
    label: '交易单号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '商户单号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '支付方式',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: computed(() => {
      return payWay.value
    }),
  },
])
const page_data = ref({
  current: 1,
  pageSize: 10,
  updateTime: '', // 付款日期
  enterpriseName: '',
  enterpriseId: '',
  memberId: '',
  memberName: '',
  createTime: '', // 下单日期
  totalAmount: '', // 充值金额
  actualAmount: '', // 付款金额
  packageName: '',
  outTradeNo: '',
  shoppingOrderNo: '', // 商户单号
  payWay: '',
})
const payWay = ref()
onMounted(() => {
  max_height.value = window.innerHeight - 320 // 不带分页的 table 高度
  EventBus.on('refresh', () => {
    top_index.value == 1 ? get_list() : get_list_1()
    ElMessage.success('刷新成功！')
  })
  dictData('1541677419486777346').then((res) => {
    payWay.value = res as any
    payWay.value.forEach((item) => {
      item.value = item.dictCode
      item.label = item.dictValue
    })
  }) // 获取字典值 支付方式
  get_upgrade_list()
  get_recharge_list()
  setTimeout(() => {
    top_index.value == 1 ? get_list() : get_list_1()
  })
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const upgrade_list = ref([])
const recharge_list = ref([])
const get_upgrade_list = () => {
  axios
    .request({
      url: serviceApi.upgradePackage_list,
      method: 'post',
      data: { current: 1, pageSize: 1000 },
    })
    .then((res) => {
      console.log(res)
      if (res.data.content.length) {
        res.data.content.forEach((element) => {
          ;(element.label = element.name), (element.value = element.name)
        })
        upgrade_list.value = res.data.content
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const get_recharge_list = () => {
  axios
    .request({
      url: serviceApi.rechargePackage_list,
      method: 'post',
      data: { current: 1, pageSize: 1000 },
    })
    .then((res) => {
      console.log(res)
      res.data.content.forEach((element) => {
        ;(element.label = element.name), (element.value = element.name)
      })
      recharge_list.value = res.data.content
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const max_height = ref()
const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.rechange_collectionDetail_list,
      method: 'post',
      data: handle_search_key(page_data.value),
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
      ElMessage.error(error)
    })
}
const get_list_1 = () => {
  loading.value = true
  let data = JSON.parse(JSON.stringify(page_data.value))
  delete data.memberName
  delete data.memberId
  axios
    .request({
      url: serviceApi.upgrade_collectionDetail_list,
      method: 'post',
      data: handle_search_key(data),
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
      ElMessage.error(error)
    })
}
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    updateTime: header_list.value[0].value,
    enterpriseName: header_list.value[1].value,
    memberName: header_list.value[1].value,
    enterpriseId: header_list.value[2].value,
    memberId: header_list.value[2].value,
    createTime: header_list.value[3].value,
    actualAmount: header_list.value[4].value,
    totalAmount: header_list.value[5].value,
    packageName: header_list.value[6].value,
    outTradeNo: header_list.value[7].value,
    shoppingOrderNo: header_list.value[8].value,
    payWay: header_list.value[9].value,
  }
  top_index.value == 1 ? get_list() : get_list_1()
}
</script>

<style lang="scss" scoped>
.title {
  font-weight: bold;
  span {
    cursor: pointer;
    padding: 10px;
  }
  .select {
    color: #3580eb;
  }
}
</style>
